<?php

namespace Mem;

use MemLib\MemBase;
use MemLib\Pdb;

/**
 * 订单通知消息表
 *
 * @version 1.0
 * @author 张云江
 */
class MemOrder_Notice_info extends MemBase
{
    public function __construct()
    {
        parent::__construct(Pdb::db_order, "t_order_notice_info");
    }

    /**
     * 插入数据
     * @param mixed $product_id 设备ID
     * @param mixed $build_id base64数据
     */
    public function insertDataWithParam($product_id, $build_id, $environment, $notification_type, $auto_renew_status)
    {
        $insert_dt = date("Y-m-d H:i:s", time());
        $data = array(
            "product_id" => $product_id,
            "build_id" => $build_id,
            "environment" => $environment,
            "notification_type" => $notification_type,
            "auto_renew_status" => $auto_renew_status,
            "insert_dt" => $insert_dt
        );
        $res = $this->addData($data);
        if ($res) {
            \Mlog::info("t_order_notice_info 插入数据成功" . $insert_dt);
        }
        return $this->getTmpID();
    }

    /**
     * 根据订单详情
     * @param mixed $device_id
     * @return array
     */
    public function getInfoWithnotice_id($notice_id)
    {
        $sql = "SELECT * FROM t_order_notice_info WHERE notice_id='{$notice_id}';";
        $data = $this->getData($sql, "getInfoWithdevice_id:{$notice_id}");
        return $data ? $data[0] : array();
    }

    /**
     * 更新信息
     * @param $updata
     * @param $wheredata
     * @return bool
     */
    public function updateDataWithParam($updata, $wheredata)
    {
        if (empty($updata) || empty($wheredata)) {
            return false;
        }

        $updata = \MemLib\PublicFunc::convertToHtmlKeyVal($updata);
        $wheredata = \MemLib\PublicFunc::convertToHtmlKeyVal($wheredata);

        $ret = $this->updateData($updata, $wheredata);

        return $ret;
    }
    /**
     * 获取应用详情
     * @return array
     */
    public function getCountDataWithAll($application_id)
    {
        $whereStr = '';
        if ($application_id > 0) {
            $whereStr = $whereStr . ' AND pclm.application_id = ' . $application_id;
        }
        $sql = "SELECT COUNT(*) AS total FROM db_order.t_order_notice_info AS pcl LEFT JOIN db_order.t_order_application_info AS pclm ON pclm.build_id = pcl.build_id WHERE pcl.notice_id != 0 {$whereStr};";
        $data = $this->getData($sql, "getCountDataWithAllapplication_id{$application_id}");
        return empty($data) ? $data : $data[0]['total'];
    }

    /**
     * 获取应用详情
     * @return array
     */
    public function getDataWithAll($application_id, $mempage)
    {
        $application_id = (int)$application_id;
        $whereStr = '';
        if ($application_id > 0) {
            $whereStr = $whereStr . ' AND pclm.application_id = ' . $application_id;
        }
        $sql = "SELECT pcl.*,pclm.application_name,pcld.product_time,pcld.product_name FROM db_order.t_order_notice_info AS pcl LEFT JOIN db_order.t_order_product_info AS pcld ON pcld.product_channel = pcl.product_id LEFT JOIN db_order.t_order_application_info AS pclm ON pclm.build_id = pcl.build_id WHERE pcl.notice_id != 0 {$whereStr} order by pcl.insert_dt desc LIMIT {$mempage->getLimitIndex()},{$mempage->pagesize};";
        $data = $this->getData($sql, "getDataWithAllapplication_id{$application_id}:AndPage:{$mempage->getLimitIndex()}:pagesize:{$mempage->pagesize}");
        return $data;
    }
}
